#!/bin/sh /etc/rc.common
# oscam  Script VER 0.10 for openwrt  by zjhzzyf

START=94

. /etc/functions.sh
path="/usr/oscam/"


kill_oscam()
{
local otherpids
local execute
otherpids=$(ps -a 2>&1 | grep "/usr/bin/oscam" | grep -v $$ | awk -F " " '{print $1}')
echo "$otherpids" | while read execute
do
kill -9 ${execute} 
done
}



oscam_start()
{

 /usr/bin/oscam -b -c $path
  echo "readdy go....."

}



start(){
echo "game  start....."
 enable=$(uci get oscam.@setting[0].enable)
 if [  "$enable" == ""  ];then
 enable=0
 fi

 [  "$enable" == "1"  ]&&oscam_start 
   interval=$(uci get oscam.@setting[0].interval)
   #interval_current=`cat /etc/crontabs/root | grep "/etc/init.d/oscam restart" | grep -oP "(?<=\*/)\d+"`
 

( cat /etc/crontabs/root | grep -v "/etc/init.d/oscam restart" ) > /tmp/crontabs 

if  [ "$enable" == "1" -a "$interval" != "0" ]; then 
	echo "*/${interval} * * * * /etc/init.d/oscam restart" >> /tmp/crontabs
	#uci delete cron.`uci show cron|grep "/etc/init.d/oscam"|cut -d "." -f2`
	#uci add cron task
	#uci set cron.@task[-1].enabled=1
	#uci set cron.@task[-1].task_Everyday=1
	#uci set cron.@task[-1].task_time=custom
	#uci set cron.@task[-1].task_minute=${interval}
	#uci set cron.@task[-1].task_name="oscam_scheduler"
	#uci set cron.@task[-1].task_task="/etc/init.d/oscam restart"
	#uci commit cron
	cat /tmp/crontabs > /etc/crontabs/root
	/etc/init.d/cron restart
#else
	#uci delete cron.`uci show cron|grep "/etc/init.d/oscam"|cut -d "." -f2`
	#uci commit cron
	
fi

cat /tmp/crontabs > /etc/crontabs/root
/etc/init.d/cron restart
rm /tmp/crontabs

}

stop() {
  kill_oscam
	echo "oscam stopped"
}


